package com.wecommodule.bpm.ext.framework.config;

import com.wecommodule.bpm.ext.framework.config.support.RestTemplateConfigSupport;
import com.wecommodule.bpm.ext.framework.config.support.HttpConfigProperties;
import okhttp3.OkHttpClient;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

/**
 * RestTemplate配置
 *
 */
@Configuration
public class RestTemplateConfig extends RestTemplateConfigSupport {

    @Bean("bpmRestTemplate")
    @NotNull
    public RestTemplate activityRestTemplate(
            @Qualifier("bpmOkHttpClient") @NotNull OkHttpClient okHttpClient,
            @NotNull @Value("${restTemplateCallConfig.bpm.rootUrl}") String rootUrl,
            @NotNull @Value("${restTemplateCallConfig.bpm.callTimeoutMS}") Long callTimeoutMS,
            @NotNull @Value("${restTemplateCallConfig.bpm.maxIdle}") Integer maxIdle,
            @NotNull @Value("${restTemplateCallConfig.bpm.keepAliveSeconds}") Long keepAliveSeconds,
            @NotNull @Value("${restTemplateCallConfig.bpm.readTimeoutMS}") Long readTimeoutMS,
            @NotNull @Value("${restTemplateCallConfig.bpm.writeTimeoutMS}") Long writeTimeoutMS,
            @NotNull @Value("${restTemplateCallConfig.bpm.connectionMS}") Long connectionMS,
            @NotNull @Value("${restTemplateCallConfig.bpm.toService}") String toService
    ) {
        HttpConfigProperties configProperties = new HttpConfigProperties();
        configProperties.setRootUri(rootUrl);
        configProperties.setCallTimeoutMS(callTimeoutMS);
        configProperties.setWriteTimeoutMS(writeTimeoutMS);
        configProperties.setConnectTimeoutMS(connectionMS);
        configProperties.setReadTimeoutMS(readTimeoutMS);
        configProperties.setMaxIdle(maxIdle);
        configProperties.setKeepAliveSeconds(keepAliveSeconds);
        configProperties.setToService(toService);
        return super.buildRestTemplate(okHttpClient, configProperties, new MediaType[]{MediaType.ALL});
    }
}
